package leetcode.code0932;

import java.util.LinkedList;
import java.util.Queue;

import leetcode.IDebug;

public class Solution2 implements IDebug {

	public int[] beautifulArray(int n) {
		int[] ans = new int[n];
		Queue<Integer> queue = new LinkedList<>();
		queue.add(1);
		while (queue.size() < n) {
			Queue<Integer> tmp = new LinkedList<>();
			int size = queue.size();
			while (size > 0) {
				int cur = queue.poll() << 1;
				queue.add(cur - 1);
				tmp.add(cur);
				size--;
			}
			queue.addAll(tmp);
		}
		int p = 0;
		while (!queue.isEmpty()) {
			int cur = queue.poll();
			if (cur <= n) {
				ans[p++] = cur;
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution2 so = new Solution2();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
